package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.query.CustomerReportQuery;
import cn.wolfcode.rbac.service.ICustomerReportService;
import cn.wolfcode.rbac.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String list(CustomerReportQuery qo, Model model){
        model.addAttribute("result",this.customerReportService.selectCustomerReport(qo));
        return "customerReport/list";
    }

    @RequestMapping("/listByBar")
    public String listByBar(CustomerReportQuery qo, Model model){
        PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        List<Map> list = pageInfo.getList();
        ArrayList xList = new ArrayList();
        ArrayList yList = new ArrayList();
        for(Map map:list){
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/listByBar";
    }

    @RequestMapping("/listByPie")
    public String listByPie(CustomerReportQuery qo, Model model){
        PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        List<Map> list = pageInfo.getList();
        ArrayList xList = new ArrayList();
        ArrayList yList = new ArrayList();
        for(Map map:list){
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/listByPie";
    }
}
